package org.unit.testmgmt.ui.table;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.swt.widgets.TableItem;

/**
 * The cell modifier.
 * 
 * @author Robert Streng
 * 
 */
public class CellModifier implements ICellModifier {
	private TableViewerBase tableViewer;

	/**
	 * @param tableViewer
	 */
	public CellModifier(TableViewerBase tableViewer) {
		super();
		this.tableViewer = tableViewer;
	}
	
	public boolean canModify(Object element, String property) {
		return true;
	}

	public Object getValue(Object element, String property) {

		// Find the index of the column
		TableViewerColumn column = tableViewer.getTableViewerColumns().getColumn(property);

		ITableEntry entry = (ITableEntry) element;
		return entry.getValue(column);
	}

	public void modify(Object element, String property, Object value) {
		TableViewerColumn column = tableViewer.getTableViewerColumns().getColumn(property);

		TableItem item = (TableItem) element;
		ITableEntry entry = (ITableEntry) item.getData();

		Object oldValue = entry.getValue(column);
		entry.setValue(column, value);

		if (value != null) {
			if (value.equals(oldValue))
				return;
		}
		
		if (value == oldValue) {
			return;
		}
		
		tableViewer.getTableEntryList().tableEntryChanged(entry, column, value, oldValue);
	}
}
